<template>
  <div>
    <div class="user-item" v-for="(item, index) in list" :key="index" @click="select(index)">
      <div class="avatar">
        <img src="../../../../assets/img/icon-avatar.png" alt="" width="50">
      </div>
      <div class="name">
        <span>{{ item.name }}</span>
        <img v-if="idx.indexOf(index) !== -1" src="../../../../assets/img/icon-select.png" alt="" width="22">
      </div>
    </div>
    <div class="fixed-btn">
      <van-button size="large" type="info" @click="toSubmit">保存</van-button>
    </div>
  </div>
</template>

<script>
import api from '@/api'
export default {
  name: 'select-student-list',
  data () {
    return {
      list: [],
      idx: []
    }
  },
  methods: {
    // 获取发送对象 teacherMessageObject
    getList () {
      let params = {
        // subsidiary: JSON.parse(sessionStorage.getItem('user')).id
        subsidiary: '1'
      }
      api.agencyGetStudent({
        params,
        callback: res => {
          this.list = res.data.data
        }
      })
    },
    select (index) {
      let i = this.idx.indexOf(index)
      if (i === -1) {
        this.idx.push(index)
      } else {
        this.idx.splice(i, 1)
      }
    },

    toSubmit () {
      let result = []
      this.idx.forEach(i => {
        result.push(this.list[i])
      })
      window.sessionStorage.setItem('studentList', JSON.stringify(result))
      this.$router.go(-1)
    }
  },
  created () {
    this.getList()
  }
}
</script>

<style scoped>
  .user-item{
    display: flex;
    padding: 10px 0 0 10px;
    align-items: center;
  }
  .user-item .avatar{
    width: 50px;
    height: 50px;
    border-radius: 50%;
    overflow: hidden;
    flex-grow: 0;
    flex-shrink: 0;
  }
  .user-item .name{
    display: flex;
    flex-grow: 1;
    flex-shrink: 1;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid rgba(0,0,0,0.3);
    height: 50px;
    margin-left: 20px;
    font-family: PingFangBlod;
    padding-right: 10px;
  }
  .fixed-btn{
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
  }
</style>
